package pong.app.utilities
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import pong.app.IPongAppSkin;
	
	public class PongAppSkin implements IPongAppSkin
	{
		private var _context:DisplayObjectContainer;
		
		
		public function PongAppSkin(context:DisplayObjectContainer)
		{
			this._context = context
		}
		
		
		
		/**
		 * TODO: test
		 */
		public function getDisplayObject(path:String):DisplayObject
		{
			if (path.indexOf(".") == -1)
			{
				// no periods in path, return direct descendant
				return this._context.getChildByName(path);
			}
			
			var parts:Array = path.split("."); // TODO: use configurable delimiter
			
			var context:DisplayObjectContainer = this._context;
			
			var pathDepth:int = parts.length - 1;
			
			for (var i:int = 0; i < pathDepth; i++)
				context = context.getChildByName(parts[i]) as DisplayObjectContainer;
			
			var displayObject:DisplayObject = context.getChildByName(parts[pathDepth]);
			
			return displayObject;
		}
		
		
		
		
		public function show(container:DisplayObjectContainer):void
		{
			if (!container.contains(this._context))
				container.addChild(this._context);
		}
		
		
		
		public function hide():void
		{
			if (this._context.parent != null)
				this._context.parent.removeChild(this._context);
		}
	}
}